package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallNotice;
import org.linlinjava.litemall.db.mapper.LitemallNoticeDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallNoticeService {
    @Resource
    private LitemallNoticeDao noticeMapper;


    public List<LitemallNotice> querySelective(String title, String content, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallNotice> query = noticeMapper.createLambdaQuery();
    	  if (!StringUtils.isEmpty(title)) {
    		  query.andLike(LitemallNotice::getTitle, "%" + title + "%");
          }
          if (!StringUtils.isEmpty(content)) {
        	  query.andLike(LitemallNotice::getContent, "%" + content + "%");
          }
          query.andEq(LitemallNotice::getDeleted, false);

          if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	  query.orderBy(sort + " " + order);
          }
          query.limit(page, limit);
          return query.select();
    	
			/*
			 * LitemallNoticeExample example = new LitemallNoticeExample();
			 * LitemallNoticeExample.Criteria criteria = example.createCriteria();
			 * 
			 * if (!StringUtils.isEmpty(title)) { criteria.andTitleLike("%" + title + "%");
			 * } if (!StringUtils.isEmpty(content)) { criteria.andContentLike("%" + content
			 * + "%"); } criteria.andDeletedEqualTo(false);
			 * 
			 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			 * example.setOrderByClause(sort + " " + order); }
			 * 
			 * PageHelper.startPage(page, limit); return
			 * noticeMapper.selectByExample(example);
			 */
    }

    public int updateById(LitemallNotice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        return noticeMapper.updateTemplateById(notice);
        //return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    public void deleteById(Integer id) {
    	LitemallNotice notice = new LitemallNotice();
    	notice.setId(id);
    	notice.setDeleted(true);
    	noticeMapper.updateTemplateById(notice);
        //noticeMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallNotice notice) {
        notice.setAddTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.insertTemplate(notice,true);
        //noticeMapper.insertSelective(notice);
    }

    public LitemallNotice findById(Integer id) {
    	return noticeMapper.single(id);
        //return noticeMapper.selectByPrimaryKey(id);
    }

    public void deleteByIds(List<Integer> ids) {
    	
    	LambdaQuery<LitemallNotice> query =noticeMapper.createLambdaQuery();
    	
    	query.andIn(LitemallNotice::getId, ids);
    	query.andEq(LitemallNotice::getDeleted, false);
    	LitemallNotice notice = new LitemallNotice();
    	notice.setDeleted(true);
    	query.updateSelective(notice);
		/*
		 * LitemallNoticeExample example = new LitemallNoticeExample();
		 * example.or().andIdIn(ids).andDeletedEqualTo(false); LitemallNotice notice =
		 * new LitemallNotice(); notice.setUpdateTime(LocalDateTime.now());
		 * notice.setDeleted(true); noticeMapper.updateByExampleSelective(notice,
		 * example);
		 */
    }
}
